package com.hxkj.controller;

import com.hxkj.domain.po.SiteUniversal;
import com.hxkj.service.UniversalService;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.Callable;

/**
 * 网站管理、站点信息控制层
 * <p>
 * Created by yaosy on 2018年6月14日17:20:14
 */
@Slf4j
@RestController
@RequestMapping("/universal")
public class UniversalController {

    @Autowired
    private UniversalService universalService;


    /**
     * 获取单条站点数据
     *
     * @param siteUniversal
     * @throws Exception
     */
    @RequestMapping(value = "/get",consumes  ="application/json;charset=UTF-8",method = RequestMethod.POST)
    public  Callable<ResultDataStruct<SiteUniversal>> get(@RequestBody SiteUniversal siteUniversal) {
        return ()-> {
            ResultDataStruct<SiteUniversal> resultDataStruct = new ResultDataStruct<>();
            try {
                resultDataStruct.setData(universalService.get(siteUniversal));
                resultDataStruct.setMessage("成功");
                resultDataStruct.setCode(200);
            }catch (CustomException e){
                resultDataStruct.setMessage(e.getMessage());
                resultDataStruct.setCode(e.getCode().getCode());
            }catch (CustomMessageException e){
                resultDataStruct.setMessage(e.getMessage());
                resultDataStruct.setCode(e.getCode());
            }catch (Exception e){
                log.error("获取站点信息出错",e);
                resultDataStruct.setMessage("获取站点信息出错");
                resultDataStruct.setCode(400);
            }
            return resultDataStruct;
        };
    }

    /**
     * 保存单条站点数据
     * @param siteUniversal
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/modify",consumes  ="application/json;charset=UTF-8",method = RequestMethod.POST)
    public  Callable<ResultMessageStruct>  modify(@RequestBody SiteUniversal siteUniversal)  throws Exception{
        return ()-> {
            ResultMessageStruct resultStruct = new ResultMessageStruct();
            try {
                universalService.modify(siteUniversal);
                resultStruct.setMessage("成功");
                resultStruct.setCode(200);
            }catch (CustomException e){
                resultStruct.setMessage(e.getMessage());
                resultStruct.setCode(e.getCode().getCode());
            }catch (CustomMessageException e){
                resultStruct.setMessage(e.getMessage());
                resultStruct.setCode(e.getCode());
            }catch (Exception e){
                log.error("修改站点信息出错",e);
                resultStruct.setMessage("修改站点信息出错");
                resultStruct.setCode(400);
            }
            return resultStruct;
        };
    }


    /**
     * 获取所有的站点配置
     * @param siteUniversal
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getList",consumes  ="application/json;charset=UTF-8",method = RequestMethod.POST)
    public  Callable<List<SiteUniversal>>  getList(@RequestBody SiteUniversal siteUniversal)  throws Exception{
        return ()-> universalService.getList(siteUniversal);
    }



}
